package com.hermit.generator.server;

import com.hermit.generator.util.DbUtil;
import com.hermit.generator.util.Field;
import com.hermit.generator.util.FreemarkerUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * 启动类  用于生成
 * 服务端口Dto
 * service
 * controller
 */
public class ServerGenerator {
//    static String MODULE = "business";
    static String MODULE = "system";
//    static String MODULE = "file";

    // 生成代码路径主地址 TODO 根据项目存放路径决定
    static String serverPath = "D:\\workspace\\student-management-system\\";
    // 控制层
    static String toControllerPath = "server\\src\\main\\java\\com\\hermit\\system\\controller\\";
    // 服务层
    static String toServicePath = "server\\src\\main\\java\\com\\hermit\\system\\service\\";
    // 实现层
    static String toServiceImplPath = "server\\src\\main\\java\\com\\hermit\\system\\service\\impl\\";
    // 实体Entity
    static String toEntityPath = "server\\src\\main\\java\\com\\hermit\\system\\entity\\";
    // 实体dto
    static String toDtoPath = "server\\src\\main\\java\\com\\hermit\\system\\dto\\";
    // Dao层
    static String toDaoPath = "server\\src\\main\\java\\com\\hermit\\system\\dao\\";
    // mapper
    static String toMapPath = "server\\src\\main\\resources\\mapper\\";

    static String generatorConfigPath = "server\\src\\main\\resources\\generator\\generatorConfig.xml";

    public static void main(String[] args) throws Exception {
        String module = MODULE;
        // 只生成配置文件中的第一个table节点
        File file = new File(serverPath + generatorConfigPath);
        SAXReader reader=new SAXReader();
        //读取xml文件到Document中
        Document doc=reader.read(file);
        //获取xml文件的根节点
        Element rootElement=doc.getRootElement();
        //读取context节点
        Element contextElement = rootElement.element("context");
        //定义一个Element用于遍历
        Element tableElement;
        //取第一个“table”的节点
        tableElement=contextElement.elementIterator("table").next();
        String Domain = tableElement.attributeValue("domainObjectName");
        String tableName = tableElement.attributeValue("tableName");
        String tableNameCn = DbUtil.getTableComment(tableName);
        String domain = Domain.substring(0, 1).toLowerCase() + Domain.substring(1);
        System.out.println("表："+tableElement.attributeValue("tableName"));
        System.out.println("Domain："+tableElement.attributeValue("domainObjectName"));


        List<Field> fieldList = DbUtil.getColumnByTableName(tableName);
        Set<String> typeSet = getJavaTypes(fieldList);

        Map<String,Object> map = new HashMap<>();
        map.put("Domain",Domain);
        map.put("domain",domain);
        map.put("tableNameCn",tableNameCn);
        map.put("module",module);
        map.put("fieldList",fieldList);
        map.put("typeSet",typeSet);
        map.put("table",tableElement.attributeValue("tableName"));

        /**生成cotroller*/
        FreemarkerUtil.initConfig("ft1_controller.ftl");
        FreemarkerUtil.generator(serverPath + toControllerPath+Domain+"Controller.java",map);
        System.out.println("生成控制层:" + Domain + "Controller.java");

        /**生成service*/
        FreemarkerUtil.initConfig("ft1_service.ftl");
        FreemarkerUtil.generator(serverPath + toServicePath+Domain+"Service.java",map);
        System.out.println("生成服务层:" + Domain + "Service.java");

        /**生成serviceImpl*/
        FreemarkerUtil.initConfig("ft1_serviceImpl.ftl");
        FreemarkerUtil.generator(serverPath + toServiceImplPath+Domain+"ServiceImpl.java",map);
        System.out.println("生成实现层:" + Domain + "ServiceImpl.java");

        /**生成entity*/
        FreemarkerUtil.initConfig("ft1_entity.ftl");
        FreemarkerUtil.generator(serverPath + toEntityPath+Domain+"Entity.java",map);
        System.out.println("生成实体层:" + Domain + "Entity.java");

        /**生成Dao*/
        FreemarkerUtil.initConfig("ft1_dao.ftl");
        FreemarkerUtil.generator(serverPath + toDaoPath+Domain+"Dao.java",map);
        System.out.println("生成Dao层:" + Domain + "Dao.java");

        /**生成map*/
        FreemarkerUtil.initConfig("ft1_map.ftl");
        FreemarkerUtil.generator(serverPath + toMapPath+Domain+"Dao.xml",map);
        System.out.println("生成Map:" + Domain + "Dao.xml");

    }

    /**
     * 获取所有java类型，用set去重
     */
    private static Set<String> getJavaTypes(List<Field> fieldList){
        Set<String> set = new HashSet<>();
        for(int i=0;i<fieldList.size();i++){
            Field field = fieldList.get(i);
            set.add(field.getJavaType());
        }
        return set;
    }
}
